//
//  PersonalCenterViewController.m
//  录音
//
//  Created by ywy on 2016/12/22.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "REPersonTableViewCell.h"
#import "REAboutViewController.h" // 关于我们
#import "RETabBarViewController.h"
#import "REHelpViewController.h" // 帮助中心
#import "REChangeViewController.h" // 修改昵称
#import "RECheckLogin.h" //
#import <SVProgressHUD.h>
#import <UIImageView+WebCache.h>

#import "RESingleton.h"
#import <MJRefresh/MJRefresh.h>


@interface PersonalCenterViewController ()<UITableViewDelegate,UITableViewDataSource,changeDelegate>

@property(nonatomic,strong) NSDictionary * userInfoDic;

@property(nonatomic,copy) NSString * dayCount;
// 本地数组
@property(nonatomic,strong)NSArray * localArr;
// 未归类的 数组
@property(nonatomic,strong) NSArray * localNotClassArr;

@end

@implementation PersonalCenterViewController
    
- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
        //自定义隐藏Tabbar
    [tabBar HideTabarView:NO];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"标题阴影"]];
    [self.baseTableView.mj_header beginRefreshing];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.localArr = [NSArray new];
    self.localNotClassArr = [NSArray new];
    self.iconImage.layer.masksToBounds = YES;
    // 给图片做描边
    self.iconImage.layer.borderWidth = 4;
    // RERGBColor(234, 234, 234)
    self.iconImage.layer.borderColor = RERGBColor(220, 220, 220) .CGColor;
    NSInteger wight = REScreenW * 100/375;
    self.iconImage.layer.cornerRadius = wight/ 2;
    //设置阴影的颜色
    self.iconImage.layer.shadowColor=RERGBColor(220, 220, 220).CGColor;
    //设置阴影的偏移量，如果为正数，则代表为往右边偏移
    self.iconImage.layer.shadowOffset=CGSizeMake(1, 1);
    //设置阴影的透明度(0~1之间，0表示完全透明)
    self.iconImage.layer.shadowOpacity=0.1;
     [self getPersonalInfo];
    //     self.titlelabel.font = [UIFont fontWithName:@"MyriadPro-BoldCond" size:20];
    self.localNumberlabel.font = [UIFont fontWithName:@"MyriadPro-BoldCond" size:40];
    self.unclassifiedNumber.font = [UIFont fontWithName:@"MyriadPro-BoldCond" size:40];
    self.serverNumber.font = [UIFont fontWithName:@"MyriadPro-BoldCond" size:40];
    
    self.dayCount = @"";
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicK)];
    [self.iconImage addGestureRecognizer:tap];
    self.baseTableView.delegate = self;
    self.baseTableView.dataSource = self;
    self.baseTableView.separatorStyle = 0;
    self.baseTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            self.localArr  =  [[RESingleton defaultCent] readModel];
            self.localNotClassArr = [[RESingleton defaultCent] readModelWithTitle:@"RE"];
        dispatch_async(dispatch_get_main_queue(), ^{
                    self.localNumberlabel.text  = [NSString stringWithFormat:@"%ld",self.localArr.count];
                    self.unclassifiedNumber.text = [NSString stringWithFormat:@"%ld",self.localNotClassArr.count];
                   [self.baseTableView.mj_header endRefreshing];
            });
        });
    }];
    [self.baseTableView.mj_header beginRefreshing];
    [self registerNib];
}
-(void)getPersonalInfo
{
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary * pram = [NSMutableDictionary new];
    pram[@"cmd"] = @"getPersonalInfo";
    pram[@"uid"] = [UserDefaults objectForKey:@"userid"];
    [SVProgressHUD showWithStatus:@"获取个人资料中!!!!"];
    [RECheckLogin POST:REBaseUrl parameters:pram progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            weakSelf.userInfoDic = responseObject[@"userInfo"];
            weakSelf.userNameLabel.text =   weakSelf.userInfoDic[@"nickName"];
            
            [weakSelf.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",   weakSelf.userInfoDic[@"userIcon"]]] placeholderImage:nil];
            weakSelf.serverNumber.text = weakSelf.userInfoDic[@"fileCount"];
            weakSelf.dayCount = weakSelf.userInfoDic[@"dayCount"];
            [SVProgressHUD dismiss];
            [weakSelf .baseTableView reloadData];
        }else
        {
//            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
          [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败!! 查看网络!!"];
    }];
}

-(void)imageClicK
{
    
    __weak typeof(self) weakSelf = self;
    REChangeViewController * changeVC = [[REChangeViewController alloc] init];

   
    changeVC.nickeNameStr = self.userInfoDic[@"nickName"];
    changeVC.changeName = ^(NSString * url, NSString * name)
    {
        weakSelf .userNameLabel.text =   name;
        [weakSelf .iconImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil];
    };
    
    [self.navigationController pushViewController:changeVC animated:YES];
}

-(void)registerNib{
        [self.baseTableView registerNib:[UINib nibWithNibName:@"REPersonTableViewCell" bundle:nil] forCellReuseIdentifier:@"REPersonTableViewCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;
    }
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        REPersonTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"REPersonTableViewCell"];
        cell.selectionStyle = 0;
        cell.minutesTextField.text = [NSString stringWithFormat:@"%@分钟",self.dayCount];
        cell.changeDelegate = self;
        cell.tapClick = ^(NSInteger number)
        {
            switch (number) {
                case 100:
                {
                    // 每日目标设置
                }
                break;
                case 101:
                {
                    // 帮助以及反馈
                    REHelpViewController * helpVC = [[REHelpViewController alloc] init];
                    [self.navigationController pushViewController:helpVC animated:YES];
                }
                break;
                case 102:
                {
                    // 关于
                    REAboutViewController * aboutVC= [[REAboutViewController alloc] init];
                    [self.navigationController pushViewController:aboutVC animated:YES];
                    
                }
                break;
                default:
                break;
            }
        };
        
        return cell;
    }
-(void)cheneMinius:(NSString *)str
{
    NSMutableDictionary * prama = [NSMutableDictionary new];
    prama[@"cmd"] = @"updateDayCountAll";
    prama[@"uid"] = [UserDefaults objectForKey:@"userid"];
    prama[@"dayCount"] = str;
    [SVProgressHUD showWithStatus:@"修改每日目标中....."];
    [RECheckLogin POST:REBaseUrl parameters:prama progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            [SVProgressHUD showSuccessWithStatus:responseObject[@"resultNote"]];
        }else
        {
//            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
          [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 170;
    }
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
